import {defineStore} from "pinia";
import useUserStore from "@/pinia/stores/user";


const useCounterStore = defineStore("counterStore", {
    state() {
        return {
            counter: 1,
            userList: [{id: 1, name: "马云", age: 22}, {id: 2, name: "马化腾", age: 23},]
        }
    },
    actions: {},
    getters: {

        // 基本使用
        doubleCounter(state) {
            return state.counter * 10
        },

        //一个getters引入另外一个getters
        doubleCounterAdd(state) {
            // return state.doubleCounter + 1
            // this指向store实例
            return this.doubleCounter + 1
        },

        // getters返回一个函数
        getUserById(state) {
            return function (id) {
                return state.userList.find(item => item.id === id)
            }
        },

        // getters中用别的store中的getters
        getUserStoreName() {
            const userStore = useUserStore()
            return userStore.name
        }
    }
})
export default useCounterStore